<template>
  <!-- 上传图片组件 -->
  <el-dialog :visible.sync="showdialog" :close-on-click-modal="false" width="785px" :show-close="false" :before-close="handleClose">
    <div class="dialogContainer">
      <div class="head">
        <span>添加图片</span>
      </div>
      <!-- 主体部分 -->
      <div class="content">
        <div class="left">
          <p>分类归属</p>
          <el-cascader v-model="selRegion" @change="selectQuery" ref="cascaderQuery" :options="options" :props="optionProps" :checkStrictly="true" clearable></el-cascader>
          <p>图片名称</p>
          <el-input v-model="input" placeholder="限制12个字" maxlength="12"></el-input>
          <div class="err">
            <span>注意：</span>
            <br />
            <span>1.点击 + 号选择图片（一次选择最多选择20张图片）。</span>
            <br />
            <span>2.图片格式当前仅支持jpg、png</span>
          </div>
        </div>
        <div class="middle"></div>
        <div class="right">
          <el-upload
            ref="upload"
            action="http://www.cyberxauto.com:8087/image/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :headers="{ token: token }"
            :data="{
              imageTypeId: this.imageTypeId,
              name: this.input,
            }"
            :limit="20"
            :auto-upload="false"
            multiple
            :on-remove="handleRemove"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </div>
      </div>
      <div class="btn">
        <button @click="cancel">取消</button>
        <button @click="submit()">上传</button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'uploadimg',
  data() {
    return {
      selRegion: null,
      token: sessionStorage.getItem('token'),
      showdialog: false, // 显示状态
      options: [], // 筛选数据
      input: '', // 输入框数据
      imageTypeId: '', // 上传图片时 的类别
      dialogImageUrl: '',
      dialogVisible: false,
      // el-cascader显示字段修改
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  methods: {
    // 打开组件
    openCom() {
      this.showdialog = true
      this.getList()
    },
    // 获取分类列表
    async getList() {
      const res = await this.$http.get('/image/type/list')
      console.log('分类列表', res)
      // this.data = res.data.data
      this.options = res.data.data
    },
    // 关闭组件
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    },
    cancel() {
      this.showdialog = false
    },
    // 图片类型分类
    selectQuery() {
      // console.log('筛选节点信息', this.$refs.cascaderQuery.getCheckedNodes()[0].data)
      console.log('筛选长度', this.$refs.cascaderQuery.checkedValue.length)
      // if (this.$refs.cascaderQuery.checkedValue.length === 0) {
      //   this.page.type = null
      // } else {
      //   const id = this.$refs.cascaderQuery.getCheckedNodes()[0].data.id
      //   this.page.type = id
      // }

      if (this.$refs.cascaderQuery.getCheckedNodes()[0]) {
        const id = this.$refs.cascaderQuery.getCheckedNodes()[0].data.id
        this.imageTypeId = id
      } else {
        this.$message({
          type: 'warning',
          message: '请选择分类类型',
          showClose: true
        })
      }
      console.log(this.page.type)
    },
    // 选择图片删除图片
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    // 成功上传回调函数
    handleSuccess(res) {
      console.log(res, '122')
      if (res.code === 200) {
        this.$message({
          type: 'success',
          message: '上传成功'
        })
        this.input = ''
        this.imageTypeId = null
        this.showdialog = false
        this.$emit('updateList')
      } else {
        this.$message({
          type: 'error',
          message: '上传失败'
        })
      }
    },
    submit() {
      this.$refs.upload.submit()
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang= 'less' scoped>
.dialogContainer {
  button {
    cursor: pointer;
  }
  .head {
    width: 100%;
    height: 40px;
    background: #eeeeee;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .content {
    padding: 20px 30px;
    height: 400px;
    display: flex;
    justify-content: space-between;
    .left {
      width: 27%;
      height: 100%;
      .err {
        margin-top: 50px;
        span {
          font-size: 14px;
          color: #999;
          line-height: 26px;
        }
      }
    }
    .middle {
      width: 1px;
      height: 100%;
      background-color: #eee;
    }
    .right {
      width: 67%;
      height: 100%;
      overflow: auto;
    }
  }
  .btn {
    padding: 0px 260px;
    height: 76px;
    display: flex;
    justify-content: space-evenly;
    button {
      width: 99px;
      height: 35px;
      background: #ffffff;
      border-radius: 20px;
      border: 1px solid #333333;
      &:nth-child(2) {
        background: #333333;
        color: #ffffff;
      }
    }
  }
}
/deep/.el-dialog__header,
/deep/.el-dialog__body {
  padding: 0;
}
/deep/.el-dialog {
  width: 865px !important;
  height: 540px;
  box-shadow: 3px 3px 11px 0px #3d3d3d;
}
/* 修改上传文件器大小 */
/deep/.el-upload--picture-card {
  width: 91px;
  height: 91px;
  line-height: 100px;
}
/deep/.el-upload-list--picture-card .el-upload-list__item {
  width: 91px;
  height: 91px;
}
</style>
